package com.fire.feign_cilent.start.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

/**
 * @Author Mr.Fire
 * @Desc
 * @Date 2021/12/5
 */
@RestController
public class TestController {


    @Autowired
    LoadBalancerClient loadBalancerClient;

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/getOrder")
    public String orderTest(){
        String order = restTemplate.getForObject("http://cloud-client/order",String.class);
        System.out.println(order);
        return order;
    }

    @GetMapping("/getOrder2")
    public String orderTest2(){
        ServiceInstance serviceInstance = loadBalancerClient.choose("feign-client");
        URI uri = URI.create(String.format("http://%s:%s", serviceInstance.getHost() , serviceInstance.getPort()));
        System.out.println(uri.toString());
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> res = restTemplate.exchange(uri.toString(), HttpMethod.GET, HttpEntity.EMPTY, String.class);
        System.out.println(res.getBody());
        return res.getBody();
    }
}
